let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    serverUrl: app.globalData.serverUrl,
    clientInfo: {},
    obj: {},
    levelArray: [
      {
        name: '全部会员',
        level: 0
      }
    ],
    levelIndex: 0,
    levelName: '',
    type: 'hidden'
  },
  calc(){
    if (this.data.clientInfo.canUsePoints < this.data.obj.points){
      wx.showModal({
        title: '提示',
        content: '您的积分余额不足抵扣',
      })
      return
    }
    this.setData({
      type: 'calc'
    })
  },
  cancel(){
    this.setData({
      type: 'hidden'
    })
  },
  confirm(){
    app.globalData.post({
      url: '/pointMall/consume',
      data: {
        shopId: 1,
        pointMallId: this.data.pointMallId
      }
    })
  },
  navShowT(e) {
    let { item } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/ticketsCreateDetail/ticketsCreateDetail?obj=${JSON.stringify(item)}&type=${item.type.substr(1)}&hideDel=true`,
    })
  },
    del(){
        wx.showModal({
            title: '提示',
            content: '确认删除？',
            success: (res) => {
                if(res.confirm){
                    app.globalData.post({
                        url: '/pointMall/destroy',
                        data: {
                          pointMallId: this.data.obj.pointMallId
                        },
                        success: () => {
                            wx.navigateBack({})
                        }
                    })
                }
            }
        })
        
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      clientInfo: app.globalData.clientInfo
    })
    app.globalData.post({
      url: '/pointMall/getPointMallInfo',
      data: {
        pointMallId: options.id
      },
      success: (data) => {
        let obj = data.data
        obj.images = JSON.parse(obj.images)
        this.setData({
          obj: data.data
        })

        app.globalData.post({
          url: '/level/get',
          data: {
            shopId: app.globalData.shopId
          },
          success: (data) => {
            let levelInfo = data.data.levelDetail ? data.data.levelDetail : []
            let convert = levelInfo.map(val => {
              return {
                name: val.name,
                level: val.level
              }
            })

            let levelName = [...this.data.levelArray, ...convert].filter(val => {
              return val.level == this.data.obj.levelRestrict
            })[0]['name']

            this.setData({
              levelName
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})